<?php 
/**
 * 视频的服务接口
 * @author Haiqin
 *
 */
namespace Common\Logic;
class MaterialVideosLogic{
	
	private $m;
	
	function __construct() {
		$this->m = D('MaterialVideos');
		$this->obj = D('ObjVideoRelation');
	}
	/**
	 * 根据视频ID或key找到视频
	 * @param string $key
	 * @return videoinfo
	 */
	public function get_video($key){
		//$map = array('id|key'=>$key);
		$map = array('key'=>$key);
		return $this->m->where($map)->find();
		
	}
	
	/**
	 * 建立视频关联关系
	 * @param unknown $fkID
	 * @param unknown $fkGroup
	 * @param unknown $post 即$_POST参数
	 */
	public function _save($fkID,$fkGroup,$post){
		$data['fk_id'] = $fkID;
		$data['fk_group'] = $fkGroup;
		$obj = $this->obj->where($data)->find();
		$data['vid'] = $post['id'];
		$data['vframe'] = $post['vframe'];
		$data['update_time'] = Date('Y-m-d H:i:s');
		if($obj){
			$data['id'] = $obj['id'];
			if(!$post['id']){
				$this->obj->where('id='.$data['id'])->delete();
			}else{
				$this->obj->save($data);
			}
			
		}else{
			if(!$post['id']) return ;
			$this->obj->add($data);
		}
	}
	
	/**
	 * 统一删除文件
	 * @param unknown $fkID
	 * @param unknown $fkGroup
	 * @return boolean
	 */
	public function del($fkID,$fkGroup){
		$map['fk_id'] = $fkID;
		$map['fk_group'] = $fkGroup;
		if($this->m->where($map)->delete()){
			return true;
		}else{
			return false;
		}
	}
}